package com.demo;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		       
		
		doPost(request, response);
		         
		         
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("in do post");
	/*Enumeration em=request.getParameterNames();
	while (em.hasMoreElements()) {
		String string = (String) em.nextElement();
		System.out.println(string);
		
		
	}*/
		
		 String userName=  request.getParameter("username");
         String  password=request.getParameter("password");
         System.out.println("user name :"+userName);
         System.out.println("password:"+password);
         if(userName.equals("hello") && password.equals("hello")) {
        	 
        	 response.sendRedirect("WelcomeServlet"); //request , response  object will be destroyed. 
        	 //and a new request will be made by client to server so new request , response object 
        	// will be created by container.
        	 
         }else {
        	RequestDispatcher rd= request.getRequestDispatcher("ErrorServlet");
        	  rd.forward(request, response);
        	 // rd.include();
        	  //request and response object wil not be destroyed  and  Sever will do extra effort for 
        	  //forwording to ErrorServlet 
         }
	}

}
